package managedbean.SCM.MaterialOrder;

import entity.SCM.MaterialOrder;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
//import javax.faces.event.PhaseEvent;
import session.stateless.SCM.MaterialOrderBeanLocal;

@ManagedBean(name = "viewMaterialOrder")
@ViewScoped
public class ViewMaterialOrderManagedBean {

    public ViewMaterialOrderManagedBean() {
    }
    @EJB
    private MaterialOrderBeanLocal materialOrderBeanLocal;
    private MaterialOrder selectedMaterialOrder;
    private String statusMessage;

    public List<MaterialOrder> getMaterialOrders() {
        return materialOrderBeanLocal.getAllMaterialOrder();
    }

    public MaterialOrder getSelectedMaterialOrder() {
        return selectedMaterialOrder;
    }

    public void setSelectedMaterialOrder(MaterialOrder selectedMaterialOrder) {
        this.selectedMaterialOrder = selectedMaterialOrder;
    }
    
    public void sendMaterialOrder(ActionEvent event){
        try{
            if(this.selectedMaterialOrder!=null){
                if(this.materialOrderBeanLocal.sendMaterialOrder(selectedMaterialOrder)){
                    statusMessage = "Successfully";
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Material Order " + this.selectedMaterialOrder.getMaterialOrderId().toString() + " send " + statusMessage, ""));
                }else{
                    statusMessage = "Unsuccessfully (check material order state)";
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "Material Order " + this.selectedMaterialOrder.getMaterialOrderId().toString() + " send " + statusMessage, ""));
                }               
            }else{
                statusMessage = "Unsuccessfully: Choose Material Order First";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
            }
            
        }catch(Exception ex){
            System.out.printf("Sent Exception");
        }
    }
    
    public void receiveMaterials(ActionEvent event){
        try{
            if(this.selectedMaterialOrder!=null){
                if(this.selectedMaterialOrder.getMaterialOrderState().equals(MaterialOrder.State.Sent)){
                    if(this.materialOrderBeanLocal.receiveMaterials(selectedMaterialOrder)){
                        statusMessage = "Successfully";
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Material Order " + this.selectedMaterialOrder.getMaterialOrderId().toString() + " process " + statusMessage, ""));
                    }else{
                        statusMessage = "Unsuccessfully";
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "Material Order " + this.selectedMaterialOrder.getMaterialOrderId().toString() + " process " + statusMessage, ""));
                    }           
                }else{
                    statusMessage = " Must be sent first";
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "Material Order " + this.selectedMaterialOrder.getMaterialOrderId().toString() +  statusMessage, ""));
                }
            }else{
                statusMessage = "Unsuccessfully: Choose Material Order First";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
            }
        }catch(Exception ex){
            System.out.printf("Sent Exception");
        }           
    }
   
    public void updateMaterialOrder(ActionEvent event)throws IOException{
        MaterialOrder mo = this.getSelectedMaterialOrder();
        if(!mo.getMaterialOrderState().equals(MaterialOrder.State.Pending)){
            statusMessage = "Unsuccessfully: Only Pending Material Order can be updated";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }else{
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedMaterialOrder", mo);
            if(mo!=null)
                System.out.print("Update material order choose selected material Order" + mo.getCurrentDate());
            FacesContext.getCurrentInstance().getExternalContext().redirect("editMaterialOrder.xhtml");
        }
    } 
    

}
